#include "axisdata.h"
#include <QIcon>
#include <QVBoxLayout>
#include <QPropertyAnimation>
#include <src/commom/stackstatuswidget.h>
#include <src/clean/common/cleanscanbottom.h>
#include <QDebug>
#include "storagedatabase.h"
axisData::axisData(QWidget *parent) : QWidget(parent)

{
    this->initUI();
    initConnect();
}
QWidget* axisData::createDataPage(const QStringList &titles) {
    QWidget *page = new QWidget;
    QGridLayout *layout = new QGridLayout(page);
    for (int i = 0; i < titles.size(); ++i) {
        axisTest *test = new axisTest(titles[i]);
        layout->addWidget(test, i/2, i%2);
    }
    page->setStyleSheet("background-color: white;");
    return page;
}


void axisData::initUI()
{


    this->setFixedSize(900, 600);
    this->setWindowIcon(QIcon(":/main/Logo"));
    this->setWindowTitle(tr("test"));
    m_stackStatusWidget = new StackStatusWidget;
    m_stackStatusWidget->setStaticIcon(":/safe/logo_normal");
    m_stackStatusWidget->setNormalButton(":/sql/1");
    m_stackStatusWidget->setTextInfo("奥瑞", "内部测试");
    m_stackStatusWidget->m_normalButton->show();
    m_stackStatusWidget->m_normalButton2->show();
    m_stackStatusWidget->m_normalButton3->show();
    m_topBackWidget = new WenliBackWidget;
    m_topBackWidget->insertWidget(m_stackStatusWidget);
    m_topBackWidget->setButtonInfo(":/safe/return");
    m_topBackWidget->setFixedHeight(150);//绿条的宽度

    m_serialSet= new serialSet;
    m_storageDatabase=new storageDatabase ;

    m_stackWidget = new QStackedWidget(this);//打开后的页面大小
    m_stackWidget->setGeometry(rect());
    m_stackWidget->lower();

    connect(&mDataTimer, SIGNAL(timeout()), this, SLOT(timerSlot()));
//    mDataTimer.start(500);

    // 在 initUI 中调用
    m_stackWidget->addWidget(createDataPage({"NO(PPM)", "NO2(PPM)", "NOx(PPM)", "流量"}));
    m_stackWidget->addWidget(createDataPage({"压力", "反应室温度", "机箱温度", "冷堆温度"}));
    m_stackWidget->addWidget(createDataPage({"转换炉温度", "PMT电压", "PMT频率", "臭氧流量"}));

    QGridLayout *vLayout = new QGridLayout;

    vLayout->addWidget(m_topBackWidget);


    vLayout->addWidget(m_stackWidget);

    vLayout->setSpacing(0);
    vLayout->setContentsMargins(0,0,0,0);
    this->setLayout(vLayout);




}

void axisData::initConnect()
{
    connect(m_topBackWidget, SIGNAL(buttonClicked()), this, SIGNAL(goToMain()));
    connect(m_topBackWidget, SIGNAL(showMin()), this, SIGNAL(showMin()));
    connect(m_topBackWidget, SIGNAL(closeWidget()), this, SIGNAL(closeWidget()));
    connect(m_stackStatusWidget, SIGNAL(normalClicked()), this, SLOT(serialPortSet()));
    connect(m_stackStatusWidget, SIGNAL(normalClicked2()), this, SLOT(goTom_axis()));
    connect(m_stackStatusWidget, SIGNAL(normalClicked3()), this, SLOT(goTom_axis2()));
//    connect(m_serialSet, SIGNAL(mySignal()), this, SLOT(onMySignal()));
//    connect(m_serialSet, SIGNAL(mySignal2()), this, SLOT(onMySignal2()));
    connect(m_serialSet, &serialSet::mySignal, this, &axisData::onMySignal);
    connect(m_serialSet, &serialSet::mySignal2, this, &axisData::onMySignal2);
    connect(m_serialSet, &serialSet::mySignal3, this, &axisData::onMySignal3);
    connect(m_serialSet, &serialSet::mySignal4, this, &axisData::onMySignal4);
    connect(m_serialSet, &serialSet::mySignal5, this, &axisData::onMySignal5);
    connect(m_serialSet, &serialSet::mySignal6, this, &axisData::onMySignal6);
    connect(m_serialSet, &serialSet::mySignal7, this, &axisData::onMySignal7);
    connect(m_serialSet, &serialSet::mySignal8, this, &axisData::onMySignal8);
    connect(m_serialSet, &serialSet::mySignal9, this, &axisData::onMySignal9);
    connect(m_serialSet, &serialSet::mySignal10, this, &axisData::onMySignal10);

}

void axisData::timerSlot()
{

}

void axisData::serialPortSet()
{
    m_serialSet->show();
}

void axisData::goTom_axis()
{
    test = (test+2) % 3;
    m_stackWidget->setCurrentIndex(test);

}

void axisData::goTom_axis2()
{
    test = (test+1) % 3;
    m_stackWidget->setCurrentIndex(test);

}

void axisData::onMySignal(double value)
{

    QMetaObject::invokeMethod(this, [this, value]() {
        m_axisTest->staticVar=value;
//        m_storageDatabase->test=value;
    });
}
void axisData::onMySignal2(double value)
{

    QMetaObject::invokeMethod(this, [this, value]() {
        m_axisTest2->staticVar=value;
//        m_storageDatabase->test2=value;
    });
}
void axisData::onMySignal3(double value)
{

     QMetaObject::invokeMethod(this, [this, value]() {
         m_axisTest3->staticVar=value;
//         m_storageDatabase->test3=value;
     });
}
void axisData::onMySignal4(double value)
{

    QMetaObject::invokeMethod(this, [this, value]() {
        m_axisTest4->staticVar=value;
//        m_storageDatabase->test4=value;
    });
}
void axisData::onMySignal5(double value)
{

     QMetaObject::invokeMethod(this, [this, value]() {
         m_axisTest5->staticVar=value;
//         m_storageDatabase->test5=value;
     });
}
void axisData::onMySignal6(double value)
{
    QMetaObject::invokeMethod(this, [this, value]() {
        m_axisTest6->staticVar=value;
//        m_storageDatabase->test6=value;
    });

}
void axisData::onMySignal7(double value)
{

     QMetaObject::invokeMethod(this, [this, value]() {
         m_axisTest7->staticVar=value;
//         m_storageDatabase->test7=value;
     });
}
void axisData::onMySignal8(double value)
{

     QMetaObject::invokeMethod(this, [this, value]() {
         m_axisTest8->staticVar=value;
//         m_storageDatabase->test8=value;
     });
}
void axisData::onMySignal9(double value)
{

     QMetaObject::invokeMethod(this, [this, value]() {
         m_axisTest9->staticVar=value;
//         m_storageDatabase->test9=value;
     });
}
void axisData::onMySignal10(double value)
{

     QMetaObject::invokeMethod(this, [this, value]() {
         m_axisTest10->staticVar=value;
//         m_storageDatabase->test10=value;
//         m_storageDatabase->date= QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss");
//         m_storageDatabase->writeData();
     });
}

